﻿using Raffles.OA.Utility;
using Raffles.OA.Data;
using Raffles.OA.WebApp.App_Start;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Threading.Tasks;

namespace Raffles.OA.WebApp.Controllers
{
    public class CommonController : BaseActionController
    {
        #region 控件绑定

        /// <summary>
        /// 获取数据状态
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public ActionResult GetEnabledStatus()
        {
            List<KeyValuePair<object, string>> dictionaryList = typeof(EnabledMarkEnum).EnumToDictionary().ToList();
            return Content(DataResult<List<KeyValuePair<object, string>>>.Success(dictionaryList).ToJson());
        }

        /// <summary>
        /// 根据字典类型获取字典值(radio、checkbox、select)
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public ActionResult GetDictionaryDetailByType(string dicType)
        {
            var dictionaryList = _dbContext.Queryable<DictionaryDetail>()
                                           .Where(x => !x.DeletedMark && x.EnabledMark && x.DicType == dicType)
                                           .Select(x => new { Key = x.DicCode, Value = x.DicValue })
                                           .ToList();
            return Content(DataResult<dynamic>.Success(dictionaryList).ToJson());
        }

        #endregion 控件绑定  

        #region 省市区

        /// <summary>
        /// 省
        /// </summary> 
        /// <returns></returns>
        public async Task<ActionResult> GetProvince()
        {
            List<Dictionary<string, object>> dictionaryList = await _dbContext.Queryable<Area>().Where(x => x.pid == 0).Select(x => new { Key = x.Id, Value = x.ext_name }).ToDictionaryListAsync();
            return Content(DataResult<List<Dictionary<string, object>>>.Success(dictionaryList).ToJson());
        }

        /// <summary>
        /// 市
        /// </summary> 
        /// <returns></returns>
        public async Task<ActionResult> GetCity(int provinceId)
        {
            List<Dictionary<string, object>> dictionaryList = await _dbContext.Queryable<Area>().Where(x => x.pid == provinceId).Select(x => new { Key = x.Id, Value = x.ext_name }).ToDictionaryListAsync();
            return Content(DataResult<List<Dictionary<string, object>>>.Success(dictionaryList).ToJson());
        }

        /// <summary>
        /// 区
        /// </summary> 
        /// <returns></returns>
        public async Task<ActionResult> GetArea(int cityId)
        {
            List<Dictionary<string, object>> dictionaryList = await _dbContext.Queryable<Area>().Where(x => x.pid == cityId).Select(x => new { Key = x.Id, Value = x.ext_name }).ToDictionaryListAsync();
            return Content(DataResult<List<Dictionary<string, object>>>.Success(dictionaryList).ToJson());
        }

        #endregion

        #region 字典绑定
        /// <summary>
        /// 下拉框字典绑定(Id/Value)
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns> 
        public async Task<ActionResult> GetDictionaryIdByCode(string code)
        {
            List<Dictionary<string, object>> dic = await _dbContext.Queryable<DictionaryDetail>()
                                                             .Where(x => x.DicType == code && !x.DeletedMark && x.EnabledMark)     
                                                             .OrderBy(x => x.DisplayOrder)
                                                             .Select(x => new { Key = x.Id, Value = x.DicValue }).ToDictionaryListAsync();
            return Content(DataResult<List<Dictionary<string, object>>>.Success(dic).ToJson());
        }

        /// <summary>
        /// 下拉框字典绑定(Code/Value)
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns> 
        public async Task<ActionResult> GetDictionaryCodeByCode(string code)
        {
            List<Dictionary<string, object>> dic = await _dbContext.Queryable<DictionaryDetail>()
                                                             .Where(x => x.DicType == code && !x.DeletedMark && x.EnabledMark)
                                                             .Select(x => new { Key = x.DicCode, Value = x.DicValue }).ToDictionaryListAsync();
            return Content(DataResult<List<Dictionary<string, object>>>.Success(dic).ToJson());
        }

        #endregion
    }
}